import Request from 'utils/request';

import ShopGoods from 'models/classify/shopGoods';

export default class ClassifyApi {
  static getAllGoodsTypeList() {
    const apiName = 'api/wehcatshop/goodsTypes';

    return Request.post(apiName).then(res => {
      let types = res.data;
      let typeList = [];
      for (let i = 0; i < types.length; i++) {
        let data = types[i];
        typeList.push({
          id: data.majorTypeId,
          name: data.majorName
        });
      }
      return typeList;
    });
  }

  static getGoodsList(typeObj,pagination){
    const apiName = 'api/wehcatshop/getGoodsListBygoodsTypeId';

    let postData = {
      majorTypeId:typeObj.id,
      limit: pagination.pageSize,
      page: pagination.currentPage
    };

    return Request.post(apiName,postData).then(res=>{
      let goodsList = res.data;
      let goodsArr = [];
      for (let i = 0; i < goodsList.length; i++) {
        let goods = goodsList[i];
        goods = new ShopGoods(goods);
        goodsArr.push(goods);
      }

      pagination.totalCount = res.rows;
      return goodsArr;
    });
  }
}
